﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;

namespace FDeal
{
    public class ThanhToanDAO : DataProvider
    {
        public ThanhToanDAO()
        { 
        }

        public ThanhToanDTO[] getDsThanhToan()
        {
            connect();
            string query = "SELECT * FROM THANHTOAN";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ThanhToanDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new ThanhToanDTO[n];
            for (i = 0; i < n; i++)
            {
                object lop = GetDataFromDataRow(dt, i);
                arr[i] = (ThanhToanDTO)lop;
            }
            return arr;
        }



        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ThanhToanDTO tt = new ThanhToanDTO();
            tt.Idhinhthuc = dt.Rows[i]["IDHINTHUC"].ToString();
            tt.Tenhinhthuc = dt.Rows[i]["TENHINHTHUC"].ToString();
            tt.Phithanhtoan = float.Parse(dt.Rows[i]["PHITHANHTOAN"].ToString());


            return (object)tt;
        }

        public void insert(ThanhToanDTO info)
        {
            string insertCommand = "INSERT INTO THANHTOAN VALUES ('" +
                info.Idhinhthuc + "','" +
                info.Tenhinhthuc + "'," +
                info.Phithanhtoan + ")";

            executeNonQuery(insertCommand);
        }

        public void update(ThanhToanDTO info)
        {
            string updateCommand = "UPDATE THANHTOAN " +
                                    "SET TENHINHTHUC = '" + info.Tenhinhthuc + "', " +
                                    " PHITHANHTOAN = " + info.Phithanhtoan + "," +

                                    " WHERE IDHINHTHUC  = '" + info.Idhinhthuc + "'";

            executeNonQuery(updateCommand);
        }

        public void delete(string idht)
        {
            string deleteCommand = "DELETE FROM THANHTOAN WHERE IDHINHTHUC = '" + idht + "'";
            executeNonQuery(deleteCommand);
        }
    }
}